{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<SecretListHeader
  @isCertTab={{eq this.tab "cert"}}
  @model={{this.backendModel}}
  @baseKey={{this.baseKey}}
  @backendCrumb={{this.backendCrumb}}
  @filter={{this.filter}}
/>
{{#let (options-for-backend this.backendType this.tab) as |options|}}
  {{#if (or this.model.meta.total (not this.isConfigurableTab))}}
    <Toolbar>
      {{#if this.model.meta.total}}
        <ToolbarFilters>
          <NavigateInput
            @enterpriseProduct="vault"
            @filterFocusDidChange={{action "setFilterFocus"}}
            @filterDidChange={{action "setFilter"}}
            @filter={{this.filter}}
            @filterMatchesKey={{this.filterMatchesKey}}
            @firstPartialMatch={{this.firstPartialMatch}}
            @baseKey={{get this.baseKey "id"}}
            @shouldNavigateTree={{options.navigateTree}}
            @placeholder={{options.searchPlaceholder}}
            @mode={{if (eq this.tab "cert") "secrets-cert" "secrets"}}
          />
          {{#if this.filterFocused}}
            {{#if this.filterMatchesKey}}
              {{#unless this.filterIsFolder}}
                <p class="input-hint">
                  <kbd>Enter</kbd>
                  to view
                  {{this.filter}}
                </p>
              {{/unless}}
            {{/if}}
            {{#if this.firstPartialMatch}}
              <p class="input-hint">
                <kbd>Tab</kbd>
                to autocomplete
              </p>
            {{/if}}
          {{/if}}
        </ToolbarFilters>
      {{/if}}

      <ToolbarActions>
        <ToolbarSecretLink
          @secret=""
          @mode="create"
          @type="add"
          @queryParams={{hash initialKey=(or this.filter this.baseKey.id) itemType=this.tab}}
          data-test-secret-create={{true}}
        >
          {{options.create}}
        </ToolbarSecretLink>
      </ToolbarActions>
    </Toolbar>
  {{/if}}

  {{#if this.model.meta.total}}
    {{#each this.model as |item|}}
      {{! Because of the component helper cannot use glimmer nested SecretList::Item }}
      {{#let (component options.listItemPartial) as |Component|}}
        <Component
          @item={{item}}
          @backendModel={{this.backendModel}}
          @backendType={{this.backendType}}
          @delete={{action "delete" item "secret"}}
          @itemPath={{concat options.modelPrefix item.id}}
          @itemType={{options.item}}
          @modelType={{@modelType}}
          @options={{options}}
          @toggleZeroAddress={{action "toggleZeroAddress" item this.backendModel}}
          @loadingToggleZeroAddress={{get this (concat "loading-" item.id)}}
        />
      {{/let}}
    {{else}}
      <div class="box is-sideless">
        {{#if this.filterFocused}}
          There are no
          {{pluralize options.item}}
          matching
          <code>{{this.filter}}</code>, press
          <kbd>ENTER</kbd>
          to add one.
        {{else}}
          There are no
          {{pluralize options.item}}
          matching
          <code>{{this.filter}}</code>.
        {{/if}}
      </div>
    {{/each}}

    <Hds::Pagination::Numbered
      @currentPage={{this.model.meta.currentPage}}
      @currentPageSize={{this.model.meta.pageSize}}
      @route={{concat "vault.cluster.secrets.backend.list" (unless this.baseKey.id "-root")}}
      @showSizeSelector={{false}}
      @totalItems={{this.model.meta.total}}
      @queryFunction={{this.paginationQueryParams}}
    />

  {{else}}
    {{#if (eq this.baseKey.id "")}}
      {{#if (and options.firstStep (not this.tab))}}
        <EmptyState @title="Get started with {{capitalize this.backendType}}" @message={{options.firstStep}}>
          <SecretLink
            @mode="create"
            @secret=""
            @queryParams={{hash initialKey=(or this.filter this.baseKey.id) itemType=this.tab}}
            class="link"
          >
            {{options.create}}
          </SecretLink>
        </EmptyState>
      {{else}}
        <EmptyState
          @title="No {{pluralize options.item}} in this backend"
          @message="{{pluralize (capitalize options.item)}}
            in this backend will be listed here.
            {{or options.message (concat 'Add a ' options.item ' to get started.')}}"
        >
          <SecretLink
            @mode="create"
            @secret=""
            @queryParams={{hash initialKey=(or this.filter this.baseKey.id) itemType=this.tab}}
            class="link"
          >
            {{options.create}}
          </SecretLink>
        </EmptyState>
      {{/if}}
    {{else}}
      {{#if this.filterIsFolder}}
        <EmptyState
          @title={{if
            (eq this.filter this.baseKey.id)
            (concat "No " (pluralize options.item) " under &quot;" this.filter "&quot;")
            (concat "No folders matching &quot;" this.filter "&quot;")
          }}
        />
      {{/if}}
    {{/if}}
  {{/if}}
{{/let}}